package com.mvvm.ktnet.ui.rili

import android.os.Bundle
import com.android.ktx.view.onClick
import com.android.ktx.view.show
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.CalendarView.*
import com.mvvm.ktnet.R
import com.mvvm.ktnet.base.myappbase.BaseDbActivity
import com.mvvm.ktnet.base.viewmodel.BaseViewModel
import com.mvvm.ktnet.databinding.AcRiliBinding
import java.util.*
import kotlin.random.Random

class RiLiAc : BaseDbActivity<BaseViewModel, AcRiliBinding>(), OnCalendarSelectListener,
    OnYearChangeListener,
    OnMonthChangeListener {
    override fun layoutId(): Int = R.layout.ac_rili

    val defaultCa by lazy { java.util.Calendar.getInstance() }

    //选择的
    private var mYear = defaultCa.get(java.util.Calendar.YEAR)
    private var mMonth = defaultCa.get(java.util.Calendar.MONTH)
    private var mDay = defaultCa.get(java.util.Calendar.DAY_OF_MONTH)

    private var mSelectYear = defaultCa.get(java.util.Calendar.YEAR)
    private var mSelectMonth = defaultCa.get(java.util.Calendar.MONTH)
    private var mSelectDay = defaultCa.get(java.util.Calendar.DAY_OF_MONTH)


    private var mMaxYear = defaultCa.get(java.util.Calendar.YEAR)
    private var mMaxMonth = defaultCa.get(java.util.Calendar.MONTH)
    private var mMaxDay = defaultCa.get(java.util.Calendar.DAY_OF_MONTH)

    private var mMinYear = defaultCa.get(java.util.Calendar.YEAR)
    private var mMinMonth = defaultCa.get(java.util.Calendar.MONTH)
    private var mMinDay = defaultCa.get(java.util.Calendar.DAY_OF_MONTH)

    private var isScheduled = false
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.calendarView.setOnCalendarSelectListener(this)
        mDatabind.calendarView.setOnYearChangeListener(this)
        mDatabind.calendarView.setOnMonthChangeListener(this)
        isScheduled = Random.nextBoolean()
        setSelectTv()
        setMaxMixDate()

        mDatabind.calendarView.setRange(mMinYear, mMinMonth, mMinDay, mMaxYear, mMaxMonth, mMaxDay)
        setNextPre(mYear, mMonth)
        mDatabind.ivNext.onClick {
            mDatabind.calendarView.scrollToNext()
        }
        mDatabind.ivPre.onClick {
            mDatabind.calendarView.scrollToPre()
        }
    }

    fun setSelectTv() {
        mDatabind.tvYear.text = mSelectYear.toString()
        mDatabind.tvMonthDay.text = "$mSelectMonth 月$mSelectDay 日"
    }

    fun setMaxMixDate() {
        val currentCalender = getCalendar(if (isScheduled) 3 else -3)
        val calendar = java.util.Calendar.getInstance()
        if (isScheduled) {//赛程
            mMaxYear = currentCalender.get(java.util.Calendar.YEAR)
            mMaxMonth = currentCalender.get(java.util.Calendar.MONTH)
            mMaxDay = currentCalender.get(java.util.Calendar.DAY_OF_MONTH)
            mMinYear = calendar.get(java.util.Calendar.YEAR)
            mMinMonth = calendar.get(java.util.Calendar.MONTH)
            mMinDay = calendar.get(java.util.Calendar.DAY_OF_MONTH)
        } else {
            mMaxYear = calendar.get(java.util.Calendar.YEAR)
            mMaxMonth = calendar.get(java.util.Calendar.MONTH)
            mMaxDay = calendar.get(java.util.Calendar.DAY_OF_MONTH)
            mMinYear = currentCalender.get(java.util.Calendar.YEAR)
            mMinMonth = currentCalender.get(java.util.Calendar.MONTH)
            mMinDay = currentCalender.get(java.util.Calendar.DAY_OF_MONTH)
        }
    }


    fun setNextPre(mSelectYear: Int, mSelectMonth: Int) {
        if (isScheduled) {
            val showNext =
                if (mSelectYear == mMaxYear) mSelectMonth in mMinMonth until mMaxMonth else mSelectMonth < mMaxMonth
            val showPre =
                if (mSelectYear == mMinYear) mSelectMonth > mMinMonth else true

            mDatabind.ivNext.show(showNext)
            mDatabind.ivPre.show(showPre)
        } else {
            val showNext =
                if (mSelectYear == mMaxYear) mSelectMonth < mMaxMonth else true
            val showPre =
                if (mSelectYear == mMaxYear) mSelectMonth in (mMinMonth + 1)..mMaxMonth else mSelectMonth > mMinMonth
            mDatabind.ivNext.show(showNext)
            mDatabind.ivPre.show(showPre)
        }
    }

    private fun getCalendar(month: Int): java.util.Calendar {
        val calendar = java.util.Calendar.getInstance()
        calendar.add(java.util.Calendar.MONTH, month)
        return calendar
    }

    override fun createObserver() {

    }

    override fun initData() {

    }

    override fun onCalendarOutOfRange(calendar: Calendar?) {

    }

    override fun onCalendarSelect(calendar: Calendar?, isClick: Boolean) {
        calendar?.let {
            mSelectYear = it.year
            mSelectMonth = it.month - 1
            mSelectDay = it.day
        }
        setNextPre(mSelectYear, mSelectMonth)
        setSelectTv()
    }

    override fun onYearChange(year: Int) {
        setNextPre(year, mSelectMonth)
    }

    override fun onMonthChange(year: Int, month: Int) {
        setNextPre(year, month - 1)
    }
}